package com.wy.springbootblog.controller;

import com.wy.springbootblog.pojo.Result;
import com.wy.springbootblog.pojo.User;
import com.wy.springbootblog.service.UserService;
import com.wy.springbootblog.validate.loginGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 用户注册
    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        boolean bool = userService.saveUser(user);
        return bool ? Result.success() : Result.error("用户注册失败");
    }

    // 用户登录
    @PostMapping("/login")
    public Result login(@RequestBody @Validated(loginGroup.class) User user) {
        return userService.login(user.getUsername(), user.getPassword());
    }
}
